﻿// -----------------------------------------------------------------------
// <copyright file="FileReader.cs" company="Hewlett-Packard">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TheoryProject
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public static class FileReader
    {
        /// <summary>
        /// Reads from file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>list of transitions</returns>
        public static List<Task1.Transition> ReadFromFile(string path)
        {
            try
            {
                FileStream fs = new FileStream(path, FileMode.Open);
                StreamReader sr = new StreamReader(fs);

                List<Task1.Transition> transitions = new List<Task1.Transition>();
                while (sr.Peek() != -1)
                {
                    string temp = sr.ReadLine();
                    string[] tempSplit = temp.Split('=', '(', ')', ',');

                    Task1.Transition temptransition = new Task1.Transition();

                    temp = tempSplit[1].Substring(1);
                    temptransition.State = int.Parse(temp);

                    temptransition.InputChar = tempSplit[2][0] == '^' ? '\0' : tempSplit[2][0];

                    temp = tempSplit[5].Substring(1);
                    temptransition.NextState = int.Parse(temp);

                    temptransition.OutputChar = tempSplit[6][0] == '^' ? '\0' : tempSplit[6][0];

                    if (tempSplit[7][0] == 'R' || tempSplit[7][0] == 'r')
                    {
                        temptransition.Direction = Task1.Direction.RIGHT;
                    }
                    else if (tempSplit[7][0] == 'L' || tempSplit[7][0] == 'l')
                    {
                        temptransition.Direction = Task1.Direction.LEFT;
                    }
                    else if (tempSplit[7][0] == 'S' || tempSplit[7][0] == 's')
                    {
                        temptransition.Direction = Task1.Direction.STAY;
                    }

                    transitions.Add(temptransition);
                }

                return transitions;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
